<template>
  <div style="line-height:60px;display:flex">
    <div style="flex: 1">
      <span :class="collapseBtnClass" style="cursor: pointer;font-size: 18px" @click="collapse"></span>
      <el-breadcrumb separator="/" style="display: inline-block;margin-left: 10px">
        <el-breadcrumb-item :to="'/'">首页</el-breadcrumb-item>
        <el-breadcrumb-item>{{currentPathName}}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-dropdown style="width: 150px;cursor: pointer;text-align: right">
      <!--            <i class="el-icon-setting" style="margin-right: 15px"></i>-->
      <div style="display: inline-block">
        <img :src="user.avatarUrl" alt="" style="width: 30px;border-radius: 50%;position: relative;top: 10px;right: 5px"/>
        <span>{{user.nickname}}</span><i class="el-icon-arrow-down" style="margin-left: 5px;margin-right: 10px;"></i>
      </div>
      <el-dropdown-menu slot="dropdown" style="width: 150px;text-align: center">
        <el-dropdown-item style="font-size: 16px">
          <router-link to="/manage/person" style="text-decoration: none;color: black">个人信息</router-link>
        </el-dropdown-item>
        <el-dropdown-item style="font-size: 16px">
          <router-link to="/mall" style="text-decoration: none;color: black">前往商城</router-link>
        </el-dropdown-item>
        <el-dropdown-item style="font-size: 16px">
          <span style="text-decoration: none" @click="logout">退出</span>
        </el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
  </div>
</template>

<script>
import Bus from '../Bus.js'
export default {
  name: "MyHeader",
  // props:{
  //   collapseBtnClass:String,
  //   collapse:Boolean,
  // },
  data(){
    return{
      user:{},
      currentPathName:'',
      collapseBtnClass:'el-icon-s-fold',
      sideWidth:200

    }
  },
  methods:{
    load(){
      this.user=JSON.parse(sessionStorage.getItem('user'))
    },
    //  点击收缩按钮触发
    collapse(){
      this.isCollapse=!this.isCollapse
      if(this.isCollapse){
        this.sideWidth=64
        this.collapseBtnClass='el-icon-s-unfold'
      }
      else{
        this.sideWidth=200
        this.collapseBtnClass='el-icon-s-fold'
      }
      Bus.$emit('sideWidth',this.sideWidth)
      Bus.$emit('isCollapse',this.isCollapse)
    },
    //点击退出登录
    logout(){
      this.$router.push('/login')
      localStorage.removeItem("userId")
      localStorage.removeItem("user")
      this.$message.success("已退出！")
    }
  },
  watch:{
    //太妙了，监听路由的变化一下就好啦
    '$route':function (a,b){
      this.currentPathName=localStorage.getItem("currentPathName")
      // this.currentPathName=a.name
      if(this.currentPathName==='首页'){
        this.currentPathName=''
      }
    }
  },
  mounted() {
    this.load()
    // Bus.$on('用户信息',(data=>{
    //   this.user=data
    // }))
    Bus.$on('更新头像',(data=>{
      // console.log('收到:',data);
      setTimeout(() => {
        this.user=JSON.parse(sessionStorage.getItem('user'))
        // console.log(1);
      }, 100);
    }))
    this.currentPathName=localStorage.getItem("currentPathName")
  }

}
</script>

<style scoped>

</style>